import mediapipe as mp
import cv2
class Hand_d:
    def __init__(self):
        self.hand_detector=mp.solutions.hands.Hands()
        self.draw=mp.solutions.drawing_utils
    def process(self,img):
        img_rgb=cv2.cvtColor(img,cv2.COLOR_BGR2RGB)
        self.res=self.hand_detector.process(img_rgb)
        if self.res.multi_hand_landmarks:
            for i in self.res.multi_hand_landmarks:
                self.draw.draw_landmarks(img,i,mp.solutions.hands.HAND_CONNECTIONS)

    def find_positions(self,img):
        h,w,c=img.shape#获取图片的h高度w宽度c颜色通道
        position={'Left':{},'Right':{}}
        if self.res.multi_hand_landmarks:
            i=0
            for hand in self.res.multi_handedness:
                score=hand.classification[0].score
                if score>=0.8:
                    label=hand.classification[0].label
                    hand_lms=self.res.multi_hand_landmarks[i].landmark
                    for id,lm in enumerate(hand_lms):
                        x,y=int(lm.x*w),int(lm.y*h)
                        position[label][id]=(x,y)
                i+=1
        return position


